package br.com.jm.scwcd.obj4;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SetSessionTimeoutServlet
 */
public class SetSessionTimeoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SetSessionTimeoutServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		String sessionId = session.getId();

		if ("true".equals(request.getParameter("invalidate"))) {
			session.invalidate();
			System.out.println("Session " + sessionId + " Invalidated!!");
		} else {
			int sessionTimeout = 0;
			if (request.getParameter("sessionTimeout") != null) {
				sessionTimeout = new Integer(
						request.getParameter("sessionTimeout"));
				session.setMaxInactiveInterval(sessionTimeout);
				System.out.println("Session " + sessionId + " set timeout to: "
						+ sessionTimeout + " seconds");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
